new function(_)
{
	var library = new base2.Package(this, {
		name: 'library',
		parent: parchment,
		imports: 'JavaScript, parchment, parchment.beret',
		exports: 'Library'
	});

	eval(this.imports);

	// The library holds stories and savefiles
	var Library = base2.Base.extend({
		constructor: function() {
			this.stories = [];
			this.savefiles = {};
			this.lastSavefile = null;
			this.expectedHash = window.location.hash;
		},

		// Store a savefile in the library
		storeSavefile: function storeSavefile(data)
		{
			// Most storage media will need base64 encoded data
			var b64data = encode_base64(data);

			// We'll be using a timestamp as both an ID and a title
			var date = new base2.JavaScript.Date2();
			var id = date.toISOString();
			// Pop up a window to enter a save title instead?
			var title = date.toLocaleTimeString();

			// Add the savefile to the list
			this.savefiles[id] = {
				id: id,
				title: title,
				data: data,
				b64data: b64data
			};
			this.lastSavefile = this.savefiles[id];

			//	Update the hash to reflect the current savefile
			window.location.hash = "#" + b64data;
	 		this.expectedHash = window.location.hash;
			var methods = {hash: true};

			// Add the savefile to persistant storage
//			if (window.globalStorage)
//			{
//				window.globalStorage[location.hostname][saveKey] = b64data;
//				methods.persist = true;
//			}

			// Add the savefile to the side bar
			// Should we instead perhaps have a hook and let the UI do this?
			if ($('#about').length)
			{
				// Create the side bar list if needed
				if (!$('#savefiles').length)
				{
					$('#about').append('<h2>Saved games</h2><dl id="savefiles"></dl>');
					$('h2').hide().fadeIn('slow');
				}

				// Add a link to restore from the savefile. A data: link could be added too, but as there's no interface to upload savefiles, is there any point?
				$('#savefiles').prepend('<dt>' + title + '</dt><dd><a href="#' + b64data + '">Restore</a></dd>');
				$('#savefiles dt:first, #savefiles dd:first').hide().fadeIn('slow');
				self = this;
				var savefileHandler = function savefileHandler() { self.restoreSavefile(id); return false; }
				$('#savefiles a:first').click(savefileHandler);
				methods.sidebar = true;
			}

			// Return all the wonderful ways we've stored the savefile :)
			return methods;
		},

		// Restore a saved game from the library
		restoreSavefile: function restoreSavefile(id)
		{
			if (this.engine)
			{
				// If no id provided, restore the latest savefile
				id = id || this.lastSavefile.id;
				savefile = this.savefiles[id];

				if (savefile)
				{
					// Set the hash
					window.location.hash = "#" + savefile.b64data;
					this.expectedHash = window.location.hash;

					// Load the data into the engine
					var beret = new Beret(this.engine);
	          			beret.load(savefile.data);
					this.zui.onPrint("Restoring from saved game: " + savefile.title + "\n");
					return savefile;
				}
			}
			return null;
		}
	});

	eval(this.exports);
};
